package com.frxuexi.visit.view


import android.widget.SearchView
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.frxuexi.lib_base.entity.VisitStatus
import com.frxuexi.lib_base.mvvm.BaseActivity
import com.frxuexi.visit.adapter.VisitStatusAdapter
import com.frxuexi.visit.adapter.VisitAdapter
import com.frxuexi.visit.VisitViewModel
import com.frxuexi.visit.databinding.ActivityVisitBinding
import dagger.hilt.android.AndroidEntryPoint

@Route(path = "/visit/VisitActivity")
@AndroidEntryPoint
class VisitActivity : BaseActivity<ActivityVisitBinding, VisitViewModel>() {
    val visitAdapter by lazy { VisitAdapter() }
    val visitStatusAdapter by lazy { VisitStatusAdapter() }

    override fun initData() {
        vm.visitsData.observe(this){
            visitAdapter.data.clear()
            visitAdapter.addData(it)
            visitAdapter.notifyDataSetChanged()
        }
    }

    override fun initView() {
        binding.headVisit.headerAdd.setOnClickListener {
            ARouter.getInstance().build("/visit/VisitAddActivity").navigation()
        }
        binding.rvVisit.apply {
            adapter=visitAdapter
            layoutManager=LinearLayoutManager(this@VisitActivity)
        }



        val statusList = listOf<VisitStatus>(
            VisitStatus("全部状态", true),
            VisitStatus("待审核", false),
            VisitStatus("已通过", false),
            VisitStatus("驳回", false),
            VisitStatus("已完成", false)
        )
        visitStatusAdapter.addData(statusList)
        binding.visitRvstatus.apply {
            adapter=visitStatusAdapter
            layoutManager=LinearLayoutManager(this@VisitActivity,RecyclerView.HORIZONTAL,false)
        }

        visitStatusAdapter.setOnItemClickListener { adapter, view, position ->
            visitStatusAdapter.data.forEach {
                it.isCheck=false
            }
            visitStatusAdapter.data.get(position).isCheck=true
            visitStatusAdapter.notifyDataSetChanged()
            if(position==0){
                vm.getVisits()
            }else{//根据状态
                vm.getVisitByStatus(statusList.get(position).status)
            }
        }

        binding.visitSv.setOnQueryTextListener(object : SearchView.OnQueryTextListener {
            override fun onQueryTextSubmit(query: String?): Boolean {

                return true
            }

            override fun onQueryTextChange(newText: String?): Boolean {
                if(newText.isNullOrEmpty()){
                    vm.getVisits()
                }else{
                    vm.getVisitByName(newText!!)
                }
                return true
            }

        })

        visitAdapter.setOnItemClickListener { adapter, view, position ->
            ARouter.getInstance().build("/visit/VisitDetailActivity").withString("vid",visitAdapter.data.get(position).id.toString())
                .navigation()
        }
    }

    override fun onResume() {
        super.onResume()
        vm.getVisits()
    }

}